package traveler.controllers;

import traveler.activities.CourseAddActivity;
import traveler.tools.ExtendedAlertDialog;
import traveler.tools.Tools;
import traveler.activities.R;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.Toast;

public class CourseAddController extends BaseController implements View.OnClickListener, OnClickListener
{
	public CourseAddController(Activity activity)
	{
		super(activity);
	}

	public void onClick(View v) {
		Log.i("CAC", "OnClick - START ");

		switch (Tools.getInstance().classNameToInt(v.getClass().toString())) {
        
        case Tools.RELATIVELAYOUT:
            Log.d("CAC::onClick", "Tools.RELATIVELAYOUT");
            ((CourseAddActivity) v.getContext()).flipNext();
            break;
            
        case Tools.LINEARLAYOUT:
        	Log.d("CAC::onClick", "Tools.LINEARLAYOUT");
        	ExtendedAlertDialog dialog = new ExtendedAlertDialog(1, v.getContext());

			dialog.setTitle(v.getContext().getResources().getString(
					R.string.course_add_step_title));
			View view = ((LayoutInflater) v.getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
					.inflate(R.layout.course_add_step, null);

			dialog.setButton(Dialog.BUTTON_NEGATIVE, "Cancel", this);
			dialog.setButton(Dialog.BUTTON_POSITIVE, "Add", this);

			dialog.setView(view);
			dialog.show();
			
			


        	break;

        
        case Tools.BUTTON:
            Log.d("CAC::onClick", "Tools.BUTTON");
            
            switch (v.getId()) {
            case 1:
                break;

            case R.id.course_add_bt_img:
            	Log.e("CAC::onClick - Button", "bouton image");
            	((CourseAddActivity) v.getContext()).startActivityForResult(Intent.createChooser(
	                    new Intent().setType("image/*").setAction(
	                            Intent.ACTION_GET_CONTENT),
	                    "Gallery"),
	                    CourseAddActivity.SELECT_PICTURE);

                break;

            case R.id.course_add_relative_manage_step:
                ((CourseAddActivity) v.getContext()).flipNext();
                break;
                
            case R.id.course_add_relative_manage_course:
                ((CourseAddActivity) v.getContext()).flipPrevious();
                break;

            default:
                Log.d("CAC::onClick", "DEFAULT Tools.BUTTON");
                break;
            }

            break;

        default:
            Log.d("CAC::onClick", "DEFAULT");
            break;
        }

        Log.d("CAC::onClick", "END");
    
	}

	public void onClick(DialogInterface d, int which) {
		Log.d("CAC::onClick(d,w)", "START");
        switch (Tools.getInstance().classNameToInt(d.getClass().toString())) {
        case Tools.EXTENDEDALERTDIALOG:
            ExtendedAlertDialog ead = (ExtendedAlertDialog) d;
            
            switch (ead.getId()) {
            case CourseAddActivity.DIALOG_PLUG_CHOOSER:
                Log.d("CAC::onClick(d,w)", "DIALOG_PLUG_CHOOSER");
                this.manageDialogPlugChooser(ead, which);
                break;
            
//            case CourseAddActivity.DIALOG_DELETE_ACTION:
//                // Traitement du dialog "Supprimer l'action"
//                Log.d("SAC::onClick(d,w)", "DIALOG_DELETE_ACTION");
//                switch (which) {
//                case DialogInterface.BUTTON_POSITIVE:
//                    Log.d("SAC::onClick(d,w)", "BUTTON_POSITIVE");
//                    // Suppression temporaire (n'affecte pas la BDD)
//                    this.view.deleteAction(this.view.getBufferAction());
//                    break;

//                case DialogInterface.BUTTON_NEGATIVE:
//                    Log.d("SAC::onClick(d,w)", "BUTTON_NEGATIVE");
//                    Toast.makeText(this.view, this.view.getString(
//                            R.string.add_scenario_toast_dialog_delete_nok),
//                            Toast.LENGTH_SHORT).show();
//                    break;
//                    
//                default:
//                    Log.d("SAC::onClick(d,w)", "UNKNOWN BUTTON");
//                    break;
//                }

                //ead.cancel();
                //break;
                
            default:
                Log.e("CAC::onClick(d,w)", "UNMANAGED ExtendedAlertDialog");
                ead.cancel();
                break;
            }
            break;

        default:
            Log.e("CAC::onClick(d,w)", "UNMANAGED dialog");
            d.cancel();
            break;
        }

        Log.d("CAC::onClick(d,w)", "END");
	}
	
	private void manageDialogPlugChooser(ExtendedAlertDialog d, int w) {
        switch (w) {
        case DialogInterface.BUTTON_POSITIVE:
        	Log.e("CAC::manageDialogPlugChooser", "BUTTON_POSITIVE");
        	CourseAddActivity activity = (CourseAddActivity)_activity;
        	activity.addAction();
        	break;

        case DialogInterface.BUTTON_NEGATIVE:
            Log.d("CAC::manageDialogPlugChooser", "BUTTON_NEGATIVE");
            break;

        default:
            Log.e("CAC::manageDialogPlugChooser", "UNKNOWN ACTION");
            break;
        }
        
        d.cancel();
    }
	
}